﻿using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NPOIDemo
{
    public class Excel
    {
        /// <summary>
        /// 获取字体样式
        /// </summary>
        /// <param name="hssfworkbook">Excel操作类</param>
        /// <param name="fontname">字体名</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="fontsize">字体大小</param>
        /// <returns></returns>
        public static IFont GetFontStyle(HSSFWorkbook hssfworkbook, string fontfamily, HSSFColor fontcolor, int fontsize)
        {
            IFont font1 = hssfworkbook.CreateFont();
            if (string.IsNullOrEmpty(fontfamily))
            {
                font1.FontName = fontfamily;
            }
            if (fontcolor != null)
            {
                font1.Color = fontcolor.GetIndex();
            }
            font1.IsItalic = true;
            font1.FontHeightInPoints = (short)fontsize;
            return font1;
        }

        /// <summary>
        /// 获取单元格样式
        /// </summary>
        /// <param name="hssfworkbook">Excel操作类</param>
        /// <param name="font">单元格字体</param>
        /// <param name="fillForegroundColor">图案的颜色</param>
        /// <param name="fillPattern">图案样式</param>
        /// <param name="fillBackgroundColor">单元格背景</param>
        /// <param name="ha">垂直对齐方式</param>
        /// <param name="va">垂直对齐方式</param>
        /// <returns></returns>
        public static ICellStyle GetCellStyle(HSSFWorkbook hssfworkbook, IFont font, HSSFColor fillForegroundColor, FillPatternType fillPattern, HSSFColor fillBackgroundColor, HorizontalAlignment ha, VerticalAlignment va)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            cellstyle.FillPattern = fillPattern;
            cellstyle.Alignment = ha;
            cellstyle.VerticalAlignment = va;
            if (fillForegroundColor != null)
            {
                cellstyle.FillForegroundColor = fillForegroundColor.GetIndex();
            }
            if (fillBackgroundColor != null)
            {
                cellstyle.FillBackgroundColor = fillBackgroundColor.GetIndex();
            }
            if (font != null)
            {
                cellstyle.SetFont(font);
            }
            //有边框
            cellstyle.BorderBottom = CellBorderType.THIN;
            cellstyle.BorderLeft = CellBorderType.THIN;
            cellstyle.BorderRight = CellBorderType.THIN;
            cellstyle.BorderTop = CellBorderType.THIN;
            return cellstyle;
        }

        /// <summary>
        /// 创建有边框，居中对齐的单元格样式
        /// </summary>
        /// <param name="hssfworkbook"></param>
        /// <returns></returns>
        public static ICellStyle GetICellStyle(HSSFWorkbook hssfworkbook)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            IFont font = hssfworkbook.CreateFont();
            font.FontHeightInPoints = 10;
            cellstyle.SetFont(font);
            cellstyle.Alignment = HorizontalAlignment.CENTER;
            cellstyle.VerticalAlignment = VerticalAlignment.CENTER;
            cellstyle.BorderBottom = CellBorderType.THIN;
            cellstyle.BorderLeft = CellBorderType.THIN;
            cellstyle.BorderRight = CellBorderType.THIN;
            cellstyle.BorderTop = CellBorderType.THIN;
            return cellstyle;
        }

        /// <summary>
        /// 创建无边框，右对齐的表头单元格样式
        /// </summary>
        /// <param name="hssfworkbook"></param>
        /// <returns></returns>
        public static ICellStyle GetICellStyle(HSSFWorkbook hssfworkbook,short fontsize)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            IFont font = hssfworkbook.CreateFont();
            font.FontHeightInPoints = fontsize;
            cellstyle.SetFont(font);
            cellstyle.Alignment = HorizontalAlignment.CENTER;
            cellstyle.VerticalAlignment = VerticalAlignment.CENTER;
            cellstyle.BorderBottom = CellBorderType.NONE;
            cellstyle.BorderLeft = CellBorderType.NONE;
            cellstyle.BorderRight = CellBorderType.NONE;
            cellstyle.BorderTop = CellBorderType.NONE;
            return cellstyle;
        }

        /// <summary>
        /// 创建无边框，居中对齐的单元格样式
        /// </summary>
        /// <param name="hssfworkbook"></param>
        /// <returns></returns>
        public static ICellStyle GetICellStyleNotBorder(HSSFWorkbook hssfworkbook)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            //cellstyle.Alignment = HorizontalAlignment.CENTER;
            cellstyle.VerticalAlignment = VerticalAlignment.CENTER;
            cellstyle.BorderBottom = CellBorderType.NONE;
            cellstyle.BorderLeft = CellBorderType.NONE;
            cellstyle.BorderRight = CellBorderType.NONE;
            cellstyle.BorderTop = CellBorderType.NONE;
            return cellstyle;
        }

        /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="rowstart"></param>
        /// <param name="rowend"></param>
        /// <param name="colstart"></param>
        /// <param name="colend"></param>
        public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
        {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
            sheet.AddMergedRegion(cellRangeAddress);
            //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(cellRangeAddress, CellBorderType.NONE, NPOI.HSSF.Util.HSSFColor.AUTOMATIC.index);
            //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(cellRangeAddress, CellBorderType.THIN, NPOI.HSSF.Util.HSSFColor.BLACK.index);
        }



    }
}
